package com.nulldev.util.networking.async2.tcp.listeners;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketException;
import java.net.SocketTimeoutException;

public class TCPListenerThread implements Runnable {

	private final ServerSocket fSocket;
	private final TCPListener fMyListener;

	public TCPListenerThread(ServerSocket socket, TCPListener myListener) {
		fSocket = socket;
		fMyListener = myListener;
	}

	@Override
	public void run() {
		while (fMyListener.isActive()) {
			try {
				TCPClientConnection client = new TCPClientConnection(fSocket.accept(), fMyListener, fMyListener.isMessageCollectorInUse(),
						fMyListener.getMessageSplitter());
				if (fMyListener.getConnectedClients().size() >= fMyListener.getMaxConnections()) {
					client.disconnectMe();
				}
			} catch (SocketTimeoutException ex) {
			} catch (SocketException ex) {
			} catch (SecurityException | IOException ex) {
			}
		}
	}
}